Imports ADOConnection
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient

Public Class CMatHang
    Private _mathang_id As Integer
    Private _ma As String
    Private _code As String
    Private _ten As String
    Private _lo As String
    Private _gle As Double
    Private _dvt As String
    Private _hansd As String
    Private _kho As String
    Private _lk As String
    Private _h As String
    Private _ngtton As Date
    Private _slss11t As Single
    Private _slkk11t As Single
    Private _slss17t As Single
    Private _slkk17t As Single
    Private _sln17t As Single
    Private _slx17t As Single
    Private _slss11 As Single
    Private _slkk11 As Single
    Private _slss17 As Single
    Private _slkk17 As Single
    Private _slndkkh As Single
    Private _slxdkkh As Single
    Private _slndkth As Single
    Private _slxdkth As Single
    Private _sltonkh As Single
    Private _sltonth As Single
    Private _sltonss As Single
    Private _hkk1 As String
    Private _hkk7 As String
    Private _sltl11 As Single
    Private _sltl17 As Single



#Region "Property"

    Public Property MatHang_ID() As Integer
        Get
            Return _mathang_id
        End Get
        Set(ByVal value As Integer)
            _mathang_id = value
        End Set
    End Property

    Public Property Ma() As String
        Get
            Return _ma
        End Get
        Set(ByVal value As String)
            _ma = value
        End Set
    End Property

    Public Property Code() As String
        Get
            Return _code
        End Get
        Set(ByVal value As String)
            _code = value
        End Set
    End Property

    Public Property Ten() As String
        Get
            Return _ten
        End Get
        Set(ByVal value As String)
            _ten = value
        End Set
    End Property

    Public Property Lo() As String
        Get
            Return _lo
        End Get
        Set(ByVal value As String)
            _lo = value
        End Set
    End Property

    Public Property Gle() As Double
        Get
            Return _gle
        End Get
        Set(ByVal value As Double)
            _gle = value
        End Set
    End Property

    Public Property DVT() As String
        Get
            Return _dvt
        End Get
        Set(ByVal value As String)
            _dvt = value
        End Set
    End Property

    Public Property HanSD() As String
        Get
            Return _hansd
        End Get
        Set(ByVal value As String)
            _hansd = value
        End Set
    End Property

    Public Property Kho() As String
        Get
            Return _kho
        End Get
        Set(ByVal value As String)
            _kho = value
        End Set
    End Property

    Public Property LK() As String
        Get
            Return _lk
        End Get
        Set(ByVal value As String)
            _lk = value
        End Set
    End Property

    Public Property H() As String
        Get
            Return _h
        End Get
        Set(ByVal value As String)
            _h = value
        End Set
    End Property

    Public Property NgTTon() As Date
        Get
            Return _ngtton
        End Get
        Set(ByVal value As Date)
            _ngtton = value
        End Set
    End Property

    Public Property SLSS11T() As Single
        Get
            Return _slss11t
        End Get
        Set(ByVal value As Single)
            _slss11t = value
        End Set
    End Property

    Public Property SLKK11T() As Single
        Get
            Return _slkk11t
        End Get
        Set(ByVal value As Single)
            _slkk11t = value
        End Set
    End Property

    Public Property SLSS17T() As Single
        Get
            Return _slss17t
        End Get
        Set(ByVal value As Single)
            _slss17t = value
        End Set
    End Property

    Public Property SLKK17T() As Single
        Get
            Return _slkk17t
        End Get
        Set(ByVal value As Single)
            _slkk17t = value
        End Set
    End Property

    Public Property SLN17T() As Single
        Get
            Return _sln17t
        End Get
        Set(ByVal value As Single)
            _sln17t = value
        End Set
    End Property

    Public Property SLX17T() As Single
        Get
            Return _slx17t
        End Get
        Set(ByVal value As Single)
            _slx17t = value
        End Set
    End Property

    Public Property SLSS11() As Single
        Get
            Return _slss11
        End Get
        Set(ByVal value As Single)
            _slss11 = value
        End Set
    End Property

    Public Property SLKK11() As Single
        Get
            Return _slkk11
        End Get
        Set(ByVal value As Single)
            _slkk11 = value
        End Set
    End Property

    Public Property SLSS17() As Single
        Get
            Return _slss17
        End Get
        Set(ByVal value As Single)
            _slss17 = value
        End Set
    End Property

    Public Property SLKK17() As Single
        Get
            Return _slkk17
        End Get
        Set(ByVal value As Single)
            _slkk17 = value
        End Set
    End Property

    Public Property SLNDKKH() As Single
        Get
            Return _slndkkh
        End Get
        Set(ByVal value As Single)
            _slndkkh = value
        End Set
    End Property

    Public Property SLXDKKH() As Single
        Get
            Return _slxdkkh
        End Get
        Set(ByVal value As Single)
            _slxdkkh = value
        End Set
    End Property

    Public Property SLNDKTH() As Single
        Get
            Return _slndkth
        End Get
        Set(ByVal value As Single)
            _slndkth = value
        End Set
    End Property

    Public Property SLXDKTH() As Single
        Get
            Return _slxdkth
        End Get
        Set(ByVal value As Single)
            _slxdkth = value
        End Set
    End Property

    Public Property SLTonKH() As Single
        Get
            Return _sltonkh
        End Get
        Set(ByVal value As Single)
            _sltonkh = value
        End Set
    End Property

    Public Property SLTonTH() As Single
        Get
            Return _sltonth
        End Get
        Set(ByVal value As Single)
            _sltonth = value
        End Set
    End Property

    Public Property SLTonSS() As Single
        Get
            Return _sltonss
        End Get
        Set(ByVal value As Single)
            _sltonss = value
        End Set
    End Property

    Public Property HKK1() As String
        Get
            Return _hkk1
        End Get
        Set(ByVal value As String)
            _hkk1 = value
        End Set
    End Property

    Public Property HKK7() As String
        Get
            Return _hkk7
        End Get
        Set(ByVal value As String)
            _hkk7 = value
        End Set
    End Property

    Public Property SLTL11() As Single
        Get
            Return _sltl11
        End Get
        Set(ByVal value As Single)
            _sltl11 = value
        End Set
    End Property

    Public Property SLTL17() As Single
        Get
            Return _sltl17
        End Get
        Set(ByVal value As Single)
            _sltl17 = value
        End Set
    End Property

#End Region

    'mathang_id, ma, code, ten, lo, gle, dvt, hansd, kho, lk, h, ngtton, slss11t, slkk11t, slss17t, slkk17t, sln17t, slx17t, slss11, slkk11, slss17, slkk17, slndk, slxdk, sltonkh, sltonth, sltonss, hkk1, hkk7

#Region "Functions"
    Private Function GetDateParam(ByVal pDate As Date) As Object
        If pDate = "1/1/2001" Then
            Return DBNull.Value
        End If
        Return pDate
    End Function

    'Lay NgTTon cua mathang khi can insert hoac update
    Public Sub UpdateNgayTaoTon(ByVal ngayDPNX As Date)
        Dim com As SqlCommand = Nothing
        Dim dt As Date = Nothing
        Try
            Dim par(1) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@mathang_id", SqlDbType.Decimal)
            par(0).Value = _mathang_id
            par(1) = New SqlParameter("@ngaydpnx", SqlDbType.SmallDateTime)
            par(1).Value = ngayDPNX
            BaseDB.ExecSqlProcedure_NoneQuery("sp_UpdateNgayTTon", par)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function IsExists() As Long
        _mathang_id = -1
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "select * from tblTON where"
        strSQL &= " ma = " & objDBUtility.GetParamPlaceHolder("ma", False)
        strSQL &= " and ten = " & objDBUtility.GetParamPlaceHolder("ten", False)
        If _lo Is Nothing Then
            strSQL &= " and lo is null"
        Else
            strSQL &= " and lo = " & objDBUtility.GetParamPlaceHolder("lo", False)
        End If

        If _hansd Is Nothing Then
            strSQL &= " and hansd is null"
        Else
            strSQL &= " and hansd = " & objDBUtility.GetParamPlaceHolder("hansd", False)
        End If

        strSQL &= " and dvt = " & objDBUtility.GetParamPlaceHolder("dvt", False)

        If _h Is Nothing Then
            strSQL &= " and h is null"
        Else
            strSQL &= " and h = " & objDBUtility.GetParamPlaceHolder("h", False)
        End If

        strSQL &= " and gle = " & objDBUtility.GetParamPlaceHolder("gle", False)
        strSQL &= " and lk = " & objDBUtility.GetParamPlaceHolder("lk", False)
        strSQL &= " and kho = " & objDBUtility.GetParamPlaceHolder("kho", False)

        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)
        Dim objReader As CDataReader = Nothing
        Try
            With objCommand
                .Parameters.Add(objDBUtility.CreateParameter("ma", DbType.String, _ma, _ma.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ten", DbType.String, _ten, _ten.Length))
                If _lo IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("lo", DbType.String, _lo, _lo.Length))
                End If
                If _hansd IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("hansd", DbType.String, _hansd, _hansd.Length))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("dvt", DbType.String, _dvt, _dvt.Length))
                If _h IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("h", DbType.String, _h, _h.Length))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("gle", DbType.Decimal, _gle, 0))
                .Parameters.Add(objDBUtility.CreateParameter("lk", DbType.String, _lk, _lk.Length))
                .Parameters.Add(objDBUtility.CreateParameter("kho", DbType.String, _kho, _kho.Length))
            End With
            objReader = objConnection.GetDataReader(objCommand, False)
            'Neu mat hang da ton tai thi lay _mathang_id va _code de hien thi tren Grid
            If objReader.Read() Then
                _mathang_id = objReader.GetValue(0) 'return mathang_id
                _code = objReader.GetValue(2)
            End If
        Catch ex As System.Exception
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return _mathang_id
    End Function

    Public Function ColumnLength(ByVal column As String) As Integer
        Return CInt(BaseDB.ExecSql_DataValue("select COL_LENGTH('tblTON','" + column + "')")) / 2
    End Function

    Public Function Insert() As Long
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility
        _code = CThuocs.GetNewCode(_ma)
        Dim objStringBuilder As New StringBuilder(250)
        With objStringBuilder
            .Append("INSERT INTO tblTON(" & _
                    "ma, code, ten, lo, gle, dvt, hansd, kho, lk, h," & _
                    "ngtton, slss11t, slkk11t, slss17t, slkk17t, sln17t," & _
                    "slx17t, slss11, slkk11, slss17, slkk17," & _
                    "slndkkh, slxdkkh, slndkth, slxdkth, sltonkh, sltonth,sltonss) VALUES(")
            .Append(objDBUtility.GetParamPlaceHolder("ma", True))
            .Append(objDBUtility.GetParamPlaceHolder("code", True))
            .Append(objDBUtility.GetParamPlaceHolder("ten", True))
            .Append(objDBUtility.GetParamPlaceHolder("lo", True))
            .Append(objDBUtility.GetParamPlaceHolder("gle", True))
            .Append(objDBUtility.GetParamPlaceHolder("dvt", True))
            .Append(objDBUtility.GetParamPlaceHolder("hansd", True))
            .Append(objDBUtility.GetParamPlaceHolder("kho", True))
            .Append(objDBUtility.GetParamPlaceHolder("lk", True))
            .Append(objDBUtility.GetParamPlaceHolder("h", True))
            .Append(objDBUtility.GetParamPlaceHolder("ngtton", True))
            .Append(objDBUtility.GetParamPlaceHolder("slss11t", True))
            .Append(objDBUtility.GetParamPlaceHolder("slkk11t", True))
            .Append(objDBUtility.GetParamPlaceHolder("slss17t", True))
            .Append(objDBUtility.GetParamPlaceHolder("slkk17t", True))
            .Append(objDBUtility.GetParamPlaceHolder("sln17t", True))
            .Append(objDBUtility.GetParamPlaceHolder("slx17t", True))
            .Append(objDBUtility.GetParamPlaceHolder("slss11", True))
            .Append(objDBUtility.GetParamPlaceHolder("slkk11", True))
            .Append(objDBUtility.GetParamPlaceHolder("slss17", True))
            .Append(objDBUtility.GetParamPlaceHolder("slkk17", True))
            .Append(objDBUtility.GetParamPlaceHolder("slndkkh", True))
            .Append(objDBUtility.GetParamPlaceHolder("slxdkkh", True))
            .Append(objDBUtility.GetParamPlaceHolder("slndkth", True))
            .Append(objDBUtility.GetParamPlaceHolder("slxdkth", True))
            .Append(objDBUtility.GetParamPlaceHolder("sltonkh", True))
            .Append(objDBUtility.GetParamPlaceHolder("sltonth", True))
            .Append(objDBUtility.GetParamPlaceHolder("sltonss", False))
            .Append(")")
        End With

        Dim sInsert As String = objStringBuilder.ToString

        Dim objCommand As IDbCommand = objConnection.CreateCommand(sInsert)
        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("ma", DbType.String, _ma, _ma.Length))
                .Parameters.Add(objDBUtility.CreateParameter("code", DbType.String, _code, _code.Length))
                .Parameters.Add(objDBUtility.CreateParameter("ten", DbType.String, _ten, _ten.Length))

                If _lo IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("lo", DbType.String, _lo, _lo.Length))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("lo", DbType.String, DBNull.Value, 0))
                End If

                .Parameters.Add(objDBUtility.CreateParameter("gle", DbType.Decimal, _gle, 0))
                .Parameters.Add(objDBUtility.CreateParameter("dvt", DbType.String, _dvt, _dvt.Length))

                If _hansd IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("hansd", DbType.String, _hansd, _hansd.Length))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("hansd", DbType.String, DBNull.Value, 0))
                End If

                .Parameters.Add(objDBUtility.CreateParameter("kho", DbType.String, _kho, _kho.Length))
                .Parameters.Add(objDBUtility.CreateParameter("lk", DbType.String, _lk, _lk.Length))
                If _h IsNot Nothing Then
                    .Parameters.Add(objDBUtility.CreateParameter("h", DbType.String, _h, _h.Length))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("h", DbType.String, DBNull.Value, 0))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("ngtton", DbType.Date, _ngtton, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slss11t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slkk11t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slss17t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slkk17t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sln17t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slx17t", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slss11", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slkk11", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slss17", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slkk17", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slndkkh", DbType.Decimal, _sltonkh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slxdkkh", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slndkth", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slxdkth", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sltonkh", DbType.Decimal, _sltonkh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sltonth", DbType.Decimal, 0, 0))
                .Parameters.Add(objDBUtility.CreateParameter("sltonss", DbType.Decimal, 0, 0))
            End With
            _mathang_id = objDBUtility.InsertAndReturnSerialKey(objCommand, "mathang_id", "tblTON")
            objTransaction.Commit()
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return _mathang_id
    End Function

    'Public Function InsertNoiBo() As Long
    '    Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
    '    Dim objDBUtility As IDBUtility = objConnection.DBUtility
    '    _code = CThuocs.GetNewCode(_ma)
    '    Dim objStringBuilder As New StringBuilder(250)
    '    With objStringBuilder
    '        .Append("INSERT INTO tblTON(" & _
    '                "ma, code, ten, lo, gle, dvt, hansd, kho, lk, h," & _
    '                "ngtton, slss11t, slkk11t, slss17t, slkk17t, sln17t," & _
    '                "slx17t, slss11, slkk11, slss17, slkk17," & _
    '                "slndkkh, slxdkkh, slndkth, slxdkth, sltonkh, sltonth,sltonss) VALUES(")
    '        .Append(objDBUtility.GetParamPlaceHolder("ma", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("code", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("ten", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("lo", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("gle", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("dvt", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("hansd", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("kho", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("lk", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("h", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("ngtton", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slss11t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slkk11t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slss17t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slkk17t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("sln17t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slx17t", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slss11", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slkk11", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slss17", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slkk17", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slndkkh", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slxdkkh", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slndkth", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("slxdkth", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("sltonkh", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("sltonth", True))
    '        .Append(objDBUtility.GetParamPlaceHolder("sltonss", False))
    '        .Append(")")
    '    End With

    '    Dim sInsert As String = objStringBuilder.ToString

    '    Dim objCommand As IDbCommand = objConnection.CreateCommand(sInsert)
    '    Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
    '    Try
    '        With objCommand
    '            .Transaction = objTransaction
    '            .Parameters.Add(objDBUtility.CreateParameter("ma", DbType.String, _ma, _ma.Length))
    '            .Parameters.Add(objDBUtility.CreateParameter("code", DbType.String, _code, _code.Length))
    '            .Parameters.Add(objDBUtility.CreateParameter("ten", DbType.String, _ten, _ten.Length))

    '            If _lo IsNot Nothing Then
    '                .Parameters.Add(objDBUtility.CreateParameter("lo", DbType.String, _lo, _lo.Length))
    '            Else
    '                .Parameters.Add(objDBUtility.CreateParameter("lo", DbType.String, DBNull.Value, 0))
    '            End If

    '            .Parameters.Add(objDBUtility.CreateParameter("gle", DbType.Decimal, _gle, 0))

    '            .Parameters.Add(objDBUtility.CreateParameter("dvt", DbType.String, _dvt, _dvt.Length))

    '            If _hansd IsNot Nothing Then
    '                .Parameters.Add(objDBUtility.CreateParameter("hansd", DbType.String, _hansd, _hansd.Length))
    '            Else
    '                .Parameters.Add(objDBUtility.CreateParameter("hansd", DbType.String, DBNull.Value, 0))
    '            End If

    '            .Parameters.Add(objDBUtility.CreateParameter("kho", DbType.String, _kho, _kho.Length))
    '            .Parameters.Add(objDBUtility.CreateParameter("lk", DbType.String, _lk, _lk.Length))
    '            If _h IsNot Nothing Then
    '                .Parameters.Add(objDBUtility.CreateParameter("h", DbType.String, _h, _h.Length))
    '            Else
    '                .Parameters.Add(objDBUtility.CreateParameter("h", DbType.String, DBNull.Value, 0))
    '            End If
    '            .Parameters.Add(objDBUtility.CreateParameter("ngtton", DbType.Date, _ngtton, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slss11t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slkk11t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slss17t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slkk17t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("sln17t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slx17t", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slss11", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slkk11", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slss17", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slkk17", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slndkkh", DbType.Decimal, _sltonkh, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slxdkkh", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slndkth", DbType.Decimal, _sltonkh, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("slxdkth", DbType.Decimal, 0, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("sltonkh", DbType.Decimal, _sltonkh, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("sltonth", DbType.Decimal, _sltonkh, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("sltonss", DbType.Decimal, _sltonkh, 0))
    '        End With
    '        _mathang_id = objDBUtility.InsertAndReturnSerialKey(objCommand, "mathang_id", "tblTON")
    '        objTransaction.Commit()
    '    Catch ex As System.Exception
    '        objTransaction.Rollback()
    '        Throw ex
    '    Finally
    '        objConnection.Close()
    '    End Try
    '    Return _mathang_id
    'End Function

    Public Function AddSLTON(ByVal slkh As Double, ByVal slth As Double) As Long
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblTON SET"
        If g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANKEHOACH) Then
            strSQL &= " slndkkh = slndkkh + " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " sltonkh = sltonkh + " & objDBUtility.GetParamPlaceHolder("slkh", False)
            strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
        ElseIf g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANTAICHINH) Then
            strSQL &= " slndkkh = slndkkh + " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " slndkth = slndkth + " & objDBUtility.GetParamPlaceHolder("slth", True)
            strSQL &= " sltonkh = sltonkh + " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " sltonth = sltonth + " & objDBUtility.GetParamPlaceHolder("slth", True)
            strSQL &= " sltonss = sltonss + " & objDBUtility.GetParamPlaceHolder("slth", False)
            strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
        End If

        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("slkh", DbType.Decimal, slkh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slth", DbType.Decimal, slth, 0))
                .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()
            End With
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return lRowsAffected
    End Function

    'Public Function AddSLTONNoiBo(ByVal sl As Double) As Long
    '    Dim lRowsAffected As Long = 0
    '    Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
    '    Dim objDBUtility As IDBUtility = objConnection.DBUtility

    '    Dim strSQL As String = "UPDATE tblTON SET"
    '    If g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANKEHOACH) Then
    '        strSQL &= " slndkkh = slndkkh + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " slndkth = slndkth + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonkh = sltonkh + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonth = sltonth + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonss = sltonss + " & objDBUtility.GetParamPlaceHolder("sl", False)
    '        strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
    '    End If

    '    Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
    '    Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

    '    Try
    '        With objCommand
    '            .Transaction = objTransaction
    '            .Parameters.Add(objDBUtility.CreateParameter("sl", DbType.Decimal, sl, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
    '            lRowsAffected = .ExecuteNonQuery()
    '            objTransaction.Commit()
    '        End With
    '    Catch ex As System.Exception
    '        objTransaction.Rollback()
    '        Throw ex
    '    Finally
    '        objConnection.Close()
    '    End Try
    '    Return lRowsAffected
    'End Function

    Public Function SubstractSLTON(ByVal slkh As Double, ByVal slth As Double) As Long
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblTON SET"
        If g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANKEHOACH) Then
            strSQL &= " slxdkkh = slxdkkh + " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " sltonkh = sltonkh - " & objDBUtility.GetParamPlaceHolder("slkh", False)
            strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
        ElseIf g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANTAICHINH) Then
            strSQL &= " slxdkkh = slxdkkh + " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " slxdkth = slxdkth + " & objDBUtility.GetParamPlaceHolder("slth", True)
            strSQL &= " sltonkh = sltonkh - " & objDBUtility.GetParamPlaceHolder("slkh", True)
            strSQL &= " sltonth = sltonth - " & objDBUtility.GetParamPlaceHolder("slth", True)
            strSQL &= " sltonss = sltonss - " & objDBUtility.GetParamPlaceHolder("slth", False)
            strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
        End If

        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("slkh", DbType.Decimal, slkh, 0))
                .Parameters.Add(objDBUtility.CreateParameter("slth", DbType.Decimal, slth, 0))
                .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()
            End With
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return lRowsAffected
    End Function

    'Public Function SubstractSLTONNoiBo(ByVal sl As Double) As Long
    '    Dim lRowsAffected As Long = 0
    '    Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
    '    Dim objDBUtility As IDBUtility = objConnection.DBUtility

    '    Dim strSQL As String = "UPDATE tblTON SET"
    '    If g_User.Ban_ID = LocPhanQuyen(enumPHANQUYEN.BANKEHOACH) Then
    '        strSQL &= " slxdkkh = slxdkkh + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " slxdkth = slxdkth + " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonkh = sltonkh - " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonth = sltonth - " & objDBUtility.GetParamPlaceHolder("sl", True)
    '        strSQL &= " sltonss = sltonss - " & objDBUtility.GetParamPlaceHolder("sl", False)
    '        strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
    '    End If

    '    Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
    '    Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

    '    Try
    '        With objCommand
    '            .Transaction = objTransaction
    '            .Parameters.Add(objDBUtility.CreateParameter("sl", DbType.Decimal, sl, 0))
    '            .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
    '            lRowsAffected = .ExecuteNonQuery()
    '            objTransaction.Commit()
    '        End With
    '    Catch ex As System.Exception
    '        objTransaction.Rollback()
    '        Throw ex
    '    Finally
    '        objConnection.Close()
    '    End Try
    '    Return lRowsAffected
    'End Function

    Public Function AddSLTONSS(ByVal sl As Single) As Long
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblTON SET"
        strSQL &= " sltonss = sltonss + " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " sltonkh = sltonkh + " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " sltonth = sltonth + " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " slss11 = slss11 + " & objDBUtility.GetParamPlaceHolder("sl", False)
        strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
       
        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("sl", DbType.Decimal, sl, 0))
                .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()
            End With
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return lRowsAffected
    End Function

    Public Function SubstractSLTONSS(ByVal slth As Single) As Long
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblTON SET"
        strSQL &= " sltonss = sltonss - " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " sltonkh = sltonkh - " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " sltonth = sltonth - " & objDBUtility.GetParamPlaceHolder("sl", True)
        strSQL &= " slss11 = slss11 - " & objDBUtility.GetParamPlaceHolder("sl", False)
        strSQL &= " WHERE MATHANG_ID = " & objDBUtility.GetParamPlaceHolder("MATHANG_ID", False)
     

        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("sl", DbType.Decimal, slth, 0))
                .Parameters.Add(objDBUtility.CreateParameter("mathang_id", DbType.Decimal, _mathang_id, 0))
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()
            End With
        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try
        Return lRowsAffected
    End Function
#End Region

End Class

Public Class CMatHangs
    'Reset lai sltonkh, sltonth, sltonss, slndkkh, slxdkkh, slndkth, slxdkth doi voi phieu nhap xuat thong thuong
    'Reset lai sltonkh, sltonth, sltonss, slss11 doi voi phieu nhap xuat la dieu chinh so sach
    Public Shared Sub UpdateSLTONMatHangByDPNX(ByVal DPNX_ID As Integer)
        Try
            Dim par(0) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = DPNX_ID
            BaseDB.ExecSqlProcedure_NoneQuery("sp_UpdateSLTONMatHangByDPNX_ID", par)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    'Lay SLTON tai mot thoi diem cu the cua mat hang
    Public Shared Sub GetSLTonDenNgay(ByVal dpnx_id As Integer, ByVal mathang_id As Integer, ByVal ngay As Date, _
        ByRef sltonkh As Double, ByRef sltonth As Double, ByRef slndkkh As Double, ByRef slxdkkh As Double)
        Dim cmd As SqlCommand = Nothing
        Try
            Dim par(6) As SqlClient.SqlParameter
            par(0) = New SqlParameter("@dpnx_id", SqlDbType.Decimal)
            par(0).Value = dpnx_id
            par(1) = New SqlParameter("@mathang_id", SqlDbType.Decimal)
            par(1).Value = mathang_id
            par(2) = New SqlParameter("ngay", SqlDbType.DateTime)
            par(2).Value = ngay.Date
            par(3) = New SqlParameter("@sltonkh", SqlDbType.Float)
            par(3).Direction = ParameterDirection.Output
            par(4) = New SqlParameter("@sltonth", SqlDbType.Float)
            par(4).Direction = ParameterDirection.Output
            par(5) = New SqlParameter("@slndkkh", SqlDbType.Float)
            par(5).Direction = ParameterDirection.Output
            par(6) = New SqlParameter("@slxdkkh", SqlDbType.Float)
            par(6).Direction = ParameterDirection.Output
            cmd = BaseDB.ExecSqlProcedure_Command("sp_HienThiSLTonDenNgay", par)
            Try
                sltonkh = cmd.Parameters("@sltonkh").Value
            Catch ex As Exception
                sltonkh = 0
            End Try

            Try
                sltonth = cmd.Parameters("@sltonth").Value
            Catch ex As Exception
                sltonth = 0
            End Try

            Try
                slndkkh = cmd.Parameters("@slndkkh").Value
            Catch ex As Exception
                slndkkh = 0
            End Try

            Try
                slxdkkh = cmd.Parameters("@slxdkkh").Value
            Catch ex As Exception
                slxdkkh = 0
            End Try
        Catch ex As Exception
            Throw ex
        Finally
            cmd.Connection.Close()
        End Try
    End Sub

    Public Shared Function GetMatHangByMa(ByVal ma_MatHang As Integer) As CMatHang
        Dim objMatHang As New CMatHang
        Dim strSQL As String

        strSQL = "SELECT mathang_id, ma, code, ten, lo, isnull(gle,0), dvt, hansd, kho, lk, h, ngtton, slss11, slkk11, slndkkh, slxdkkh, slndkth, slxdkth, sltonkh, sltonth, sltonss" & _
                " FROM tblTon" & _
                " WHERE mathang_id = '" & ma_MatHang.ToString() & "'"

        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection

        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)
        Dim objReader As CDataReader = Nothing

        Try
            objReader = objConnection.GetDataReader(objCommand, False)
            While objReader.Read
                With objMatHang
                    .MatHang_ID = objReader.GetDecimal(0)
                    .Ma = objReader.GetString(1)
                    .Code = objReader.GetString(2)
                    .Ten = objReader.GetString(3)
                    .Lo = objReader.GetString(4)
                    .Gle = objReader.GetDecimal(5)
                    .DVT = objReader.GetString(6)
                    .HanSD = objReader.GetString(7)
                    .Kho = objReader.GetString(8)
                    .LK = objReader.GetString(9)
                    .H = objReader.GetString(10)
                    .NgTTon = objReader.GetDateTime(11)
                    .SLSS11 = objReader.GetDecimal(12)
                    .SLKK11 = objReader.GetDecimal(13)
                    .SLNDKKH = objReader.GetDecimal(14)
                    .SLXDKKH = objReader.GetDecimal(15)
                    .SLNDKTH = objReader.GetDecimal(16)
                    .SLXDKTH = objReader.GetDecimal(17)
                    .SLTonKH = objReader.GetDecimal(18)
                    .SLTonTH = objReader.GetDecimal(19)
                    .SLTonSS = objReader.GetDecimal(20)
                End With
            End While

        Catch ex As System.Exception
            Throw ex
        Finally
            If Not IsNothing(objReader) Then
                objReader.Close()
            End If
            objConnection.Close()
        End Try
        Return objMatHang
    End Function

    Public Shared Function GetMatHangList(ByVal strWhere As String) As ArrayList
        Dim aryItems As New ArrayList

        Dim strSQL As String
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objUtility As IDBUtility = objConnection.DBUtility

        strSQL = "SELECT mathang_id, ma, code, ten, lo, isnull(gle,0), dvt, hansd, kho, lk, h, ngtton, slndkkh, slxdkkh, slndkth, slxdkth, sltonkh, sltonth, sltonss,slkk11,slss11" & _
                " FROM viewTon " & strWhere & " order by ten, hansd asc"
        Dim objReader As CDataReader = Nothing
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)
        Try
            objReader = objConnection.GetDataReader(objCommand, True)
            While objReader.Read
                Dim objItem As New CMatHang
                With objItem
                    .MatHang_ID = objReader.GetDecimal(0)
                    .Ma = objReader.GetString(1)
                    .Code = objReader.GetString(2)
                    .Ten = objReader.GetString(3)
                    .Lo = objReader.GetString(4)
                    .Gle = objReader.GetDecimal(5)
                    .DVT = objReader.GetString(6)
                    .HanSD = objReader.GetString(7)
                    .Kho = objReader.GetString(8)
                    .LK = objReader.GetString(9)
                    .H = objReader.GetString(10)
                    .NgTTon = objReader.GetDateTime(11)
                    .SLNDKKH = objReader.GetDecimal(12)
                    .SLXDKKH = objReader.GetDecimal(13)
                    .SLNDKTH = objReader.GetDecimal(14)
                    .SLXDKTH = objReader.GetDecimal(15)
                    .SLTonKH = objReader.GetDecimal(16)
                    .SLTonTH = objReader.GetDecimal(17)
                    .SLTonSS = objReader.GetDecimal(18)
                    .SLKK11 = objReader.GetDecimal(19)
                    .SLSS11 = objReader.GetDecimal(20)
                End With
                aryItems.Add(objItem)
            End While

        Catch ex As System.Exception
            Throw ex
        Finally
            If Not IsNothing(objReader) Then
                objReader.Close()
            End If
            objConnection.Close()
        End Try
        Return aryItems
    End Function

    Public Shared Function GetDataSet(ByVal strWhere As String) As DataSet
        Dim ds As DataSet
        Dim strSQL As String
        strSQL = "SELECT mathang_id, ma, code, ten, lo, isnull(gle,0), dvt, hansd, kho, lk, h, slss11, slkk11, slndkkh, slxdkkh, sltonkh" & _
                " FROM tblTon " & strWhere & " order by ten asc"
        ds = BaseDB.ExecSql_DataSet(strSQL)
        Return ds
    End Function

    Public Shared Function UpdateKiemKe(ByVal objTB As CMatHang) As Long
        'cap nhat so lieu kiem ke : dau ki & giua ki
        Dim lRowsAffected As Long = 0
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim strSQL As String = "UPDATE tblTON SET"
        strSQL &= " slss11 = " & objDBUtility.GetParamPlaceHolder("SLSS11", True)
        strSQL &= " slkk11 = " & objDBUtility.GetParamPlaceHolder("SLKK11", True)
        strSQL &= " hkk1 = " & objDBUtility.GetParamPlaceHolder("HKK1", True)
        strSQL &= " slss17 = " & objDBUtility.GetParamPlaceHolder("SLSS17", True)
        strSQL &= " slkk17 = " & objDBUtility.GetParamPlaceHolder("SLKK17", True)
        strSQL &= " hkk7 = " & objDBUtility.GetParamPlaceHolder("HKK7", True)
        strSQL &= " sltl11 = " & objDBUtility.GetParamPlaceHolder("SLTL11", True)
        strSQL &= " sltl17 = " & objDBUtility.GetParamPlaceHolder("SLTL17", False)
        strSQL &= " WHERE mathang_id = " & objDBUtility.GetParamPlaceHolder("MatHang_ID", False)

        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim objCommand As IDbCommand = objConnection.CreateCommand(strSQL)

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("SLSS11", DbType.Double, objTB.SLSS11, 0))
                .Parameters.Add(objDBUtility.CreateParameter("SLKK11", DbType.Double, objTB.SLKK11, 0))
                If (objTB.HKK1 Is Nothing) Then
                    .Parameters.Add(objDBUtility.CreateParameter("HKK1", DbType.String, DBNull.Value, 1))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("HKK1", DbType.String, objTB.HKK1, 1))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("SLSS17", DbType.Double, objTB.SLSS17, 0))
                .Parameters.Add(objDBUtility.CreateParameter("SLKK17", DbType.Double, objTB.SLKK17, 0))
                If (objTB.HKK7 Is Nothing) Then
                    .Parameters.Add(objDBUtility.CreateParameter("HKK7", DbType.String, DBNull.Value, 1))
                Else
                    .Parameters.Add(objDBUtility.CreateParameter("HKK7", DbType.String, objTB.HKK7, 1))
                End If
                .Parameters.Add(objDBUtility.CreateParameter("MatHang_ID", DbType.Int32, objTB.MatHang_ID, 0))
                .Parameters.Add(objDBUtility.CreateParameter("SLTL11", DbType.Double, objTB.SLTL11, 0))
                .Parameters.Add(objDBUtility.CreateParameter("SLTL17", DbType.Double, objTB.SLTL17, 0))
                'Call objDBUtility.Prepare(objCommand)
                lRowsAffected = .ExecuteNonQuery()
                objTransaction.Commit()
            End With

        Catch ex As System.Exception
            objTransaction.Rollback()
            Throw ex
        Finally
            objConnection.Close()
        End Try

        Return lRowsAffected

    End Function

    Public Shared Function DeleteMatHang(ByVal objMH As CMatHang) As Long
        Dim objConnection As IADOConnection = g_objConnFactory.GetConnection
        Dim objDBUtility As IDBUtility = objConnection.DBUtility

        Dim sDelete As String = "DELETE FROM tblTON WHERE (mathang_id = "
        sDelete &= objDBUtility.GetParamPlaceHolder("MatHangID", False)
        sDelete &= ")"

        Dim objCommand As IDbCommand = objConnection.CreateCommand(sDelete)
        Dim objTransaction As IDbTransaction = objConnection.BeginTransaction
        Dim lRowsAffected As Long = 0

        Try
            With objCommand
                .Transaction = objTransaction
                .Parameters.Add(objDBUtility.CreateParameter("MatHangID", DbType.Int32, objMH.MatHang_ID, 0))
                'Call objDBUtility.Prepare(objCommand)
                lRowsAffected = .ExecuteNonQuery()

            End With

            'lSerialKey = objDBUtility.InsertAndReturnSerialKey(objCommand, "DoiTuong_ID", "DoiTuong")
            objTransaction.Commit()

        Catch ex As System.Exception
            objTransaction.Rollback()
            'Throw ex
        Finally
            objConnection.Close()
        End Try

        Return lRowsAffected
    End Function
End Class
